<script setup lang="ts">
import { useRole } from "./hook";
import { ref, defineProps, watch } from "vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { deviceDetection } from "@pureadmin/utils";
import { PureTableBar } from "@/components/RePureTableBar";
import Refresh from "@iconify-icons/ep/refresh";
const treeRef = ref();
const props = defineProps({
  formModel: {
    type: Object,
    required: true,
    default: () => ({ lists: [] })
  }
});
const emit = defineEmits(["update:formModel"]);
const newFormInline = ref({ ...props.formModel });
watch(
  () => newFormInline.value,
  newValue => {
    emit("update:formModel", newValue);
  },
  { deep: true }
);
const handleSelectionChange = val => {
  newFormInline.value.lists = val;
};
const {
  form,
  isShow,
  loading,
  tableData,
  columns,
  pagination,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange
} = useRole(treeRef);
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="表名称：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入表名称"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar
      :class="[isShow && !deviceDetection() ? '!w-[60vw]' : 'w-full']"
      style="transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1)"
      title=""
      :columns="columns"
      @refresh="onSearch"
    >
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          :data="tableData"
          :columns="dynamicColumns"
          :loading="loading"
          :size="size"
          :pagination="pagination"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
          @selection-change="handleSelectionChange"
        />
      </template>
    </PureTableBar>
  </div>
</template>
